#include <bits/stdc++.h>
#define int unsigned long long 
using namespace std;

const int maxn = 2e5+10;
char s[maxn];
int nex[maxn];
int n;

void solve(){
	cin>>n;
	string t;
	getline(cin, t);
	cin>>(s+1);
	
	nex[0]=nex[1]=0;
	for (int i=2, j=0;i<=n;i++) {
		while(j&&s[i]!=s[j+1])j=nex[j];
		if (s[i]==s[j+1]) {
			j++;
		}
		nex[i]=j;
	}
	
	int ans=0;
	for (int i=2;i<=n;i++) {
		ans=max(ans, nex[i]);
	}
	cout<<ans<<'\n';
	
}


signed main(){
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	solve();
	return 0;
}
